package com.gitee.wsl.unit.byte

import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.LongAsStringSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

class DataSizeSerializer : KSerializer<ByteUnit<*>> {
    private val delegateSerializer = LongAsStringSerializer
    override val descriptor = PrimitiveSerialDescriptor("DataSize", PrimitiveKind.LONG)

    override fun serialize(encoder: Encoder, value: ByteUnit<*>) {
        delegateSerializer.serialize(encoder, value.value)
    }

    override fun deserialize(decoder: Decoder): ByteUnit<*> {
        return ByteUnit.generateBitUnit(delegateSerializer.deserialize(decoder))
    }
}